<!-- 系统负载卡片组件 -->
<template>
  <el-card class="system-load-card">
    <template #header>
      <div class="card-header">
        <span>系统负载</span>
        <el-tag :type="statusType" size="small">{{ systemLoad.status }}</el-tag>
      </div>
    </template>
    <div class="load-info">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-progress
            type="dashboard"
            :percentage="Math.round(systemLoad.cpuUsage || 0)"
            :color="getDynamicColor(systemLoad.cpuUsage)"
          >
            <template #default>
              <div class="progress-content">
                <div>CPU</div>
                <div>{{ Math.round(systemLoad.cpuUsage || 0) }}%</div>
              </div>
            </template>
          </el-progress>
        </el-col>
        <el-col :span="8">
          <el-progress
            type="dashboard"
            :percentage="Math.round(systemLoad.memoryUsage || 0)"
            :color="getDynamicColor(systemLoad.memoryUsage)"
          >
            <template #default>
              <div class="progress-content">
                <div>内存</div>
                <div>{{ Math.round(systemLoad.memoryUsage || 0) }}%</div>
              </div>
            </template>
          </el-progress>
        </el-col>
        <el-col :span="8">
          <el-progress
            type="dashboard"
            :percentage="Math.round(systemLoad.diskUsage || 0)"
            :color="getDynamicColor(systemLoad.diskUsage)"
          >
            <template #default>
              <div class="progress-content">
                <div>磁盘</div>
                <div>{{ Math.round(systemLoad.diskUsage || 0) }}%</div>
              </div>
            </template>
          </el-progress>
        </el-col>
      </el-row>
      <div class="status-message">
        {{ systemLoad.message || '加载中...' }}
      </div>
      <div class="detail-info">
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="info-item">
              <div class="label">总内存</div>
              <div class="value">{{ formatMemory(systemLoad.totalMemory) }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <div class="label">已用内存</div>
              <div class="value">{{ formatMemory(systemLoad.usedMemory) }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <div class="label">空闲内存</div>
              <div class="value">{{ formatMemory(systemLoad.freeMemory) }}</div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="info-item">
              <div class="label">总磁盘</div>
              <div class="value">{{ formatDisk(systemLoad.totalDisk) }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <div class="label">已用磁盘</div>
              <div class="value">{{ formatDisk(systemLoad.usedDisk) }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <div class="label">空闲磁盘</div>
              <div class="value">{{ formatDisk(systemLoad.freeDisk) }}</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { getSystemLoad } from '@/api/system'
import type { SystemLoadVO } from '@/api/system'

const systemLoad = ref<SystemLoadVO>({} as SystemLoadVO)
const refreshInterval = ref<number | null>(null)

// 获取系统负载数据
const fetchSystemLoad = async () => {
  try {
    const { data } = await getSystemLoad()
    systemLoad.value = data
  } catch (error) {
    console.error('获取系统负载数据失败:', error)
  }
}

// 根据状态获取标签类型
const statusType = computed(() => {
  switch (systemLoad.value.status) {
    case 'NORMAL':
      return 'success'
    case 'WARNING':
      return 'warning'
    case 'DANGER':
      return 'danger'
    default:
      return 'info'
  }
})

// 获取动态颜色
const getDynamicColor = (percentage: number) => {
  if (percentage >= 90) {
    return '#F56C6C'
  } else if (percentage >= 70) {
    return '#E6A23C'
  } else {
    return '#67C23A'
  }
}

// 格式化内存大小
const formatMemory = (size?: number) => {
  if (!size) return '0 MB'
  if (size < 1024) {
    return size.toFixed(2) + ' MB'
  } else {
    return (size / 1024).toFixed(2) + ' GB'
  }
}

// 格式化磁盘大小
const formatDisk = (size?: number) => {
  if (!size) return '0 GB'
  if (size < 1024) {
    return size.toFixed(2) + ' GB'
  } else {
    return (size / 1024).toFixed(2) + ' TB'
  }
}

onMounted(() => {
  fetchSystemLoad()
  // 每30秒刷新一次数据
  refreshInterval.value = window.setInterval(() => {
    fetchSystemLoad()
  }, 30000)
})

onUnmounted(() => {
  if (refreshInterval.value) {
    clearInterval(refreshInterval.value)
  }
})
</script>

<style lang="scss" scoped>
.system-load-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .load-info {
    padding: 20px 0;

    .el-progress {
      margin-bottom: 20px;
    }

    .progress-content {
      text-align: center;
      font-size: 14px;
      
      div:first-child {
        color: #909399;
        margin-bottom: 5px;
      }
      
      div:last-child {
        font-weight: bold;
      }
    }
  }

  .status-message {
    text-align: center;
    margin: 15px 0;
    padding: 10px;
    background-color: #f5f7fa;
    border-radius: 4px;
    color: #606266;
  }

  .detail-info {
    margin-top: 20px;

    .info-item {
      text-align: center;
      margin-bottom: 15px;

      .label {
        color: #909399;
        font-size: 12px;
        margin-bottom: 5px;
      }

      .value {
        font-size: 14px;
        font-weight: bold;
        color: #606266;
      }
    }
  }
}
</style> 